package com.glory.brepda.adapter;

/**
 * FileName: SampleListAdapter
 * Author: XJH
 * Date: 2020/7/27 13:54
 * Description:
 * History:
 */

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.entity.BinInfoEntity;
import com.glory.brepda.entity.SimpleEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
public class SampleListAdapter extends BaseQuickAdapter<SimpleEntity.MantissaLotBinData, BaseViewHolder> {

    private int mMainQTY = -1;
    private boolean mIsSuccess = false;
    private int mSelectorPos = -1;

    public SampleListAdapter(@Nullable List<SimpleEntity.MantissaLotBinData> data,boolean isSuccess) {
        super(R.layout.item_bin_info_list,data);
        mIsSuccess = isSuccess;
    }

    @Override
    protected void convert(BaseViewHolder helper, SimpleEntity.MantissaLotBinData item) {
        CheckBox checkBox = helper.getView(R.id.cbCheck);
        checkBox.setVisibility(mIsSuccess ? View.VISIBLE : View.GONE);
        checkBox.setChecked(item.isChecked());
        helper.setText(R.id.list_tv1,item.getBINNAME());
        helper.setText(R.id.list_tv2,item.getBINDESC());
        helper.setText(R.id.list_tv3,String.valueOf(item.getMAINQTY()));
        helper.setGone(R.id.list_tv4,true);
        calculation(helper.getView(R.id.list_tv4),item.getMAINQTY());
        int position = helper.getAdapterPosition();
        checkBox.setOnClickListener(view -> {
            if (!item.getBINTYPE().equals("P")){
                checkBox.setChecked(false);
                Toast.makeText(mContext, "只能选择PASSBIN", Toast.LENGTH_SHORT).show();
                return;
            }
            if (checkBox.isChecked()){
                if (mSelectorPos == -1){
                    mSelectorPos = position;
                }else {
                    getData().get(mSelectorPos).setChecked(false);
                    notifyItemChanged(mSelectorPos);
                    mSelectorPos = position;
                }
            }else {
               mSelectorPos = -1;
            }
        });

    }

    public int getSelectorData(){
        return mSelectorPos ;
    }

    private void calculation(TextView view, double binQTY) {
        if (mMainQTY == -1){
            return;
        }
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(6);
        String result = numberFormat.format(((float) (binQTY) / (float) mMainQTY) * 100);
        view.setText(result + "%");
    }

    public void setMainQTY(int mainQTY){
        this.mMainQTY = mainQTY;
    }
}

